// Testscript SPI interface for humidity & temeratur sensor DC-SS500 from Sure Electronic
//

#include "SPI.h" // not Spi.h !!! use arduino-0022 Version !

// SPI.h defines
//#define SCK_PIN 13 (Mega = 52) to sensor board PIN 9 (SCK)
//#define MISO_PIN 12 (Mega = 50) to sensor board PIN 10 (SDO)
//#define MOSI_PIN 11 (Mega = 51) to sensor board PIN 11 (SDI)
//#define SS_PIN 10 (Mega = 53) to sensor board PIN 5 (/CS)

#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define SS_PIN 53

#define READ_HUMIDITY 0xa0 // command read humidity (8 bit signed) in %RH
#define READ_TEMPERATURE 0xb0 // command read temperature (8 bit signed) in Celsius
#define READ_TEMPERATURE_F 0xb1 // command read temperature (8 bit signed) in Fahrenheit
#define READ_STATUS 0xc0 // read sensor status, returns 0xAF when system is in idle

byte humidity; //byte to store the humidity
byte temperature; //byte to store the temperature
byte sensor_status; //byte to store the sensor status


void setup()
{
    pinMode(SS_PIN, OUTPUT); // set select_DC-SS500 to output
    digitalWrite(SS_PIN, HIGH); // disable sensor SPI interface
    
    // configure SPI
    // SPI.mode( _BV(MSTR) | _BV(CPOL) | _BV(SPE) | _BV(CPHA) | _BV(SPR0)); // B01011101 (old version for Spi.h)
    SPI.setClockDivider(SPI_CLOCK_DIV8); // divide the arduino clock (16MHz) by 8
    SPI.setDataMode(SPI_MODE3); // set spi mode = 3 (Clock Polarity (CPOL)=1; Clock Phase (CPHA)=1)
    SPI.setBitOrder(MSBFIRST) ; // data shifted in Most Significant Bit first mode
    SPI.begin(); // start the SPI library
    
    Serial.begin(9600); // send info to serial port
    Serial.println("Start DCSS500 Humidity Sensor Demo....");
    Serial.print("SPCR Mode for Sensor SPI :");
    Serial.println(SPCR, BIN);
    
    digitalWrite(SS_PIN, LOW); // Enable sensor /CS
    delay(500); // wait (sometimes nessasarry when /SS was HIGH !)
    
    Serial.print("wait for sensor status idle: ");
    for (int i = 0; i <= 60; i +=1) { // sends a lot of SENSOR_STATUS commands and wait for 0xAF
        sensor_status = SPI.transfer(READ_STATUS); // send command: "get SENSOR_STATUS" (read response)
        delay(20); // wait
        if (sensor_status == 0xAF && SPI.transfer(READ_STATUS)== 0xAF ) // break when sensor sends two times : i am idle
            break;
        Serial.print(".");
    } // end for

    Serial.println();
    if (sensor_status == 0xAF)
    Serial.println("Sensor ok (in idle mode)");
    else
    Serial.println("Something is wrong with the sensor!");
}

void loop()
{
    digitalWrite(SS_PIN, LOW); // Enable sensor SPI
    // delay(2500); // wait 2.5sec (only nessasarry when /SS was HIGH, or not? !)
    
    SPI.transfer(READ_STATUS); // send command: "get humidity" (nothing to read)
    delayMicroseconds(4000); // Wait a bit
    
    sensor_status = SPI.transfer(READ_HUMIDITY); // send command: "get humidity" & read sensor_status (the command before!)
    delayMicroseconds(4000); // Wait a bit
    
    humidity = SPI.transfer(READ_TEMPERATURE); // send command "get temperature" & read humidity (the command before!)
    delayMicroseconds(4000); // Wait a bit
    
    temperature = SPI.transfer(READ_TEMPERATURE); // send any command, read temperature
    
    
    Serial.print("Status : 0x");
    Serial.print( sensor_status, HEX); // 0xAF means idle (OK)
    Serial.print(" -- Temp : 0x"); //
    Serial.print( temperature, HEX) ; // Temperature in HEX
    Serial.print(" - ");
    Serial.print( temperature, DEC) ; // Temperature in decimal
    Serial.print(" C, Humidity : 0x");
    Serial.print( humidity, HEX) ; // Humidity in HEX
    Serial.print(" - ");
    Serial.print( humidity, DEC); // Humidity in decimal
    Serial.println(" %RH");
    digitalWrite(SS_PIN, HIGH); // disable sensor SPI (no necessary when there is only one SPI device)
    delay(1000); // wait a little bit
}
